import mongoose from './mongo.js'

const userSchema = new mongoose.Schema({
    name: { // 用户名
        type: String,
        default: '你是一名用户鸭'
    },
    avatar: { // 头像
        type: String,
        default: '/avatar.jpeg'
    },
    account: { // 账号
        type: String,
        required: true
    },
    password: { // 密码
        type: String,
        required: true
    },
    is_login: { // 是否登录
        type: Boolean,
        default: false
    },
    bio: { // 简介
        type: String,
        default: ''
    },
    space: [{ // 空间动态
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Space',
        default: [],
    }],
    group: [{ // 加入的群聊
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Group',
        default: [],
    }],
    history: [{ // 最近的消息
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Message',
        default: [],
    }],
    remakes: [{
        name: {type: String, required: true},
        user: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}
    }],
    groups: [{
        name: {type: String, required: true},
        members: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
    }]
})

const spaceSchema = new mongoose.Schema({
    user: { // 用户
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    content: { // 内容
        type: String,
        default: ''
    },
    images: [{ // 图片
        type: String,
        default: []
    }],
    videos: [{
        type: String,
        default: []
    }],
    likes: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        default: []
    }],
    comments: [{ // 评论
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Comment',
        default: []
    }],
})

const commentSchema = new mongoose.Schema({
    user: { // 用户
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    content: { // 内容
        type: String,
        default: ''
    },
    space: { // 发布的空间
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Space',
        default: ''
    },
    video: { // 发布的视频
        type: String,
        default: ''
    },
})

const messageSchema = new mongoose.Schema({
    type: {
        type: String,
        enum: ['text', 'img', 'video'],
        require: true,
    },
    is_group: { // 是否是群聊，是那就是群聊
        type: Boolean,
        require: true,
    },
    group_name: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Group',
        default: ''
    },
    title: { // 文本消息
        type: String,
        default: ''
    },
    img: {
        type: String,
        default: ''
    },
    video: {
        type: String,
        default: ''
    },
    user: { // 发送者
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        default: ''
    },
    to_user: { // 对方用户
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        default: ''
    }
}, {
    timestamps: true
})

const groupSchema = new mongoose.Schema({
    name: {
        type: String,
        require: true,
    },
    account: {
        type: String,
        default: ''
    },
    members: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        default: [],
    }],
    avatar: {
        type: String,
        default: ''
    },
    master: { // 群主
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    },
    bio: { // 简介
        type: String,
        default: ''
    },
    admins: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        default: [],
    },
})

const informSchema = new mongoose.Schema({
    title: {
        type: String,
        default: ''
    },
    sender: {  // 发送方
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    receiver: { // 接收方
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        default: ''
    },
    group_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Group',
        default: null
    },
    group: {
        type: String,
        default: '好友'
    },
    remake: {
        type: String,
        default: ''
    },
    is_space: {
        type: Boolean,
        default: false
    },
    type: {
        type: String,
        enum: ['linkman', 'group'],
        default: 'linkman'
    },
    status: {
        type: String,
        enum: ['agree', 'refuse', 'wait'],
        default: 'wait',
    }
})

const videoSchema = new mongoose.Schema({
    // 点赞数量
    likes: Number,
    //发送视频的用户
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    },
    // 图片地址
    imageUrl: [{
        type: String,
        default: []
    }],
    // 视频地址
    videoUrl: [{
        type: String,
        default: []
    }],
    // 内容描述
    content: String,
    title: String,
    // 评论列表
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Videocomments',
        }
    ]
});

const videoCommentsSchema = new mongoose.Schema({
    // 评论日期
    date: Date,
    // 发表评论的用户
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    },
    // 评论内容
    content: String,
    // 评论点赞数量
    likes: Number,
})
let FzSchema = new mongoose.Schema({
    src: String,
    title: String,
    price: String,
    vipPrice: String,
    shopDescription: String,
    delivery: String,
    shopName: String,
})

const pcVideoSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
    },
    cover: {
        type: String,
        default: '/cover.jpg'
    },
    src: {
        type: String,
        default: ''
    },
    bio: {
        type: String,
        default: ''
    },
}, {
    timestamps: true
})
let fzDB = mongoose.model("fz", FzSchema, "fz")
let newsSchema = new mongoose.Schema({
    senderId: String,
    senderId2: String,
    message: String,
    product: {
        ref: "fz",
        type: mongoose.Types.ObjectId
    },

})
let newsDB = mongoose.model("news", newsSchema, "news")
const User = mongoose.model('User', userSchema, 'user')
const Space = mongoose.model('Space', spaceSchema, 'space')
const Comment = mongoose.model('Comment', commentSchema, 'comment')
const Message = mongoose.model('Message', messageSchema, 'message')
const Group = mongoose.model('Group', groupSchema, 'group')
const Inform = mongoose.model('Inform', informSchema, 'inform')
const Video = mongoose.model('Video', videoSchema, 'Video')
const VideoComments = mongoose.model('VideoComments', videoCommentsSchema, 'VideoComments')
const pcVideo = mongoose.model('pcVideo', pcVideoSchema, 'pcVideo')

export {
    User,
    Space,
    Comment,
    Message,
    Group,
    Inform,
    VideoComments,
    Video,
    newsDB,
    pcVideo,
}